package controller.parameterpanels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JTextField;

import model.IManufacturingMethodWithPartProcessesAndTools;
import model.auxillary.ParameterCollection;
import model.auxillary.ParameterList;
import model.auxillary.ParameterList.ParameterType;
import view.AlphaTextField;
import view.CenterTabbedPane;
import view.NumericalTextField;
import view.parameterpanels.ToolParameterPanel;
import controller.CtrInformationPanel;

/**
 * The class {@code CtrToolPanel} specifies the class
 * {@code CtrInputParameterPanel} to control the behavior of a tool
 * input parameter panel. 
 * @author Oliver Eckstein
 *
 */
public class CtrToolPanel extends CtrInputParameterPanel {	
	
	/**
     * To create a new {@code CtrToolPanel}.
     * @param manuMethod the manufacturing method connected to the controller.
     * @param infoPanelCtr the information panel controller.
     * @param parameterType the type of parameter whose panel gets created.
     * @param logoPath a path to an optional logo that is shown on the
     * parameter panel.
     */
	public CtrToolPanel(
			IManufacturingMethodWithPartProcessesAndTools manuMethod,
			CtrInformationPanel infoPanelCtr,
			ParameterType parameterType,
			String logoPath) {
		super(manuMethod, infoPanelCtr, parameterType, logoPath);		
	}
	
	@Override
	public void createView(String logoPath) {
		paramPanel = new ToolParameterPanel(
				logoPath,
				CenterTabbedPane.parameterTypeToTab(parameterType),
				ParameterList.typeToString(parameterType),
				(IManufacturingMethodWithPartProcessesAndTools) manuMethod);
		ToolParameterPanel toolPanel =
				(ToolParameterPanel) paramPanel;
		
		// To add listeners to the buttons that add or remove a tool.
		toolPanel.getAddToolButton().addActionListener(addToolListener());
		toolPanel.getRemoveToolButton().addActionListener(removeToolListener());
	}
	
	@Override
	public void changeBorderText(AlphaTextField textField, String newText) {		
		
		// To change the border text when the user renamed the tool.
		ToolParameterPanel toolPanel = (ToolParameterPanel) paramPanel;
		int panelIndex = toolPanel.getToolNameTextFields().indexOf(textField);
		paramPanel.setBorderText(panelIndex, newText);
	}

	@Override
	protected void init() {
		for(ParameterCollection tool : manuMethod.getParameters().getTools()) {
			addParameterPanel(tool);
		}		
	}

	@Override
	protected void addParameterPanel(ParameterCollection collection) {		
		ToolParameterPanel toolPanel = (ToolParameterPanel) paramPanel;
		addTextFieldListeners(
				toolPanel.addParameterSet(
						createParamInfo(collection),
						collection.getName(),
						collection.getID()));
		
	}

	@Override
	protected void addTextFieldListeners(
			List<JTextField> textFields) {
		// To add text field listeners to the numerical text fields and
		// the text fields that rename a tool/border text.
		List<JTextField> numTextFields =
				new ArrayList<JTextField>();
		List<AlphaTextField> alphaTextFields = 
				new ArrayList<AlphaTextField>();
		
		for(JTextField textField : textFields) {
			if(textField instanceof NumericalTextField) {
				numTextFields.add((NumericalTextField) textField);
			} else if(textField instanceof AlphaTextField) {
				alphaTextFields.add((AlphaTextField) textField);
			}
		}
		
		super.addTextFieldListeners(numTextFields);
    	
		String paramID;    	
    	for(AlphaTextField textField : alphaTextFields) {
    		paramID = textField.getParameterID();
    		ParameterCollection tool = 
    				manuMethod.getParameters().getTool(paramID);
    		textField.addDocumentListener(new ListenerToolTextField(
    				textField,
    				tool,
    				manuMethod.getParameters(),
    				this));
    	}		
	}

	@Override
	public void changeBorderText(String newText) {
		// Nothing to do here since the border's text depends on the
		// geometry sets' names and not on the manufacturing method's name.		
	}
		
	/**
	 * To create a listener for the button that adds a new tool.
	 * @return The button's listener.
	 */
	private ActionListener addToolListener() {
		return new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				addParameterPanel(manuMethod.addTool());
				paramPanel.revalidate();				
			}
			
		};
	}
	
	/**
	 * To create a listener for the button that removes a tool.
	 * @return The button's listener.
	 */
	private ActionListener removeToolListener() {
		return new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// There must be at least one tool.
				if(manuMethod.getParameters().getTools().size() > 1) {
					removeParameterPanel();
					manuMethod.removeTool();				
					paramPanel.revalidate();	
				}				
			}
		};
	}
}
